import Config from './config.js'
import Util from './util.js'
import siteConfig from './siteConfig.js';
// #ifdef H5
const app_type = 'h5';
const app_type_name = 'H5';
// #endif

// #ifdef MP-WEIXIN
const app_type = 'weapp';
const app_type_name = '微信小程序';
// #endif


export default {
	async getUserInfo() {
		const dealer_info = uni.getStorageSync('dealer_info');
		if (dealer_info) {
			// return dealer_info;
		}
		const res = await this.sendRequest({
			url: 'Index/getuser'
		})
		if (res.code === 1) {
			uni.setStorageSync('dealer_info', res.data);
			return res.data;
		} else {
			uni.removeStorageSync('token');
			uni.removeStorageSync('dealer_info');
			uni.reLaunch({
				url: "/pages/login/login"
			})
		}
	},
	sendRequest(params) {
		if (!Config.baseUrl) {
			uni.showToast({
				title: '未配置请求域名',
				'icon': 'none',
				duration: 10000
			});
			return;
		}
		var isNonLoading = params.isNonLoading? params.isNonLoading:false;
		if(!isNonLoading){
			uni.showLoading({
				title: '加载中',
				mask: true
			});
		}
		var method = params.data != undefined ? 'POST' : 'GET', // 请求方式
			url = Config.baseUrl + params.url, // 请求路径
			data = {
				app_type,
				app_type_name
			};

		var token = uni.getStorageSync('token') || '';
		var header = params.header;
		if (!header) {
			header = {}
		}
		header['lr-dealer-token'] = token
		header['content-type'] = 'application/json'
		params.async = false
		// 参数
		if (params.data != undefined) Object.assign(data, params.data);
		if (params.async === false) {
			//同步
			return new Promise((resolve, reject) => {
				uni.request({
					url: url,
					method: method,
					data: data,
					header: header,
					dataType: params.dataType || 'json',
					responseType: params.responseType || 'text',
					success: (res) => {
						uni.hideLoading();
						if (res.data.code == 401) {
							uni.removeStorageSync('token');
							uni.removeStorageSync('dealer_info');
							uni.redirectTo({
								url: "/agentSystem/pages/login"
							})
						}
						resolve(res.data);
					},
					fail: (res) => {
						uni.hideLoading();
						if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
							uni.showToast({
								title: Config.baseUrl + '不在request 合法域名列表中',
								'icon': 'none',
								duration: 10000
							});
							return;
						}
						reject(res);
					},
					complete: (res) => {
						if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200,
								500
							].indexOf(res.statusCode) == -1)) {
							uni.showToast({
								title: Config.baseUrl + '请求失败',
								'icon': 'none',
								duration: 10000
							});
							return;
						}
						reject(res.data);
					}
				});
			});
		} else {
			//异步
			uni.request({
				url: url,
				method: method,
				data: data,
				header: header,
				dataType: params.dataType || 'json',
				responseType: params.responseType || 'text',
				success: (res) => {
					uni.hideLoading();
					if (res.data.code == 401) {
						uni.removeStorageSync('token');
						uni.reLaunch({
							url: "/agentSystem/pages/login"
						})
					}
					typeof params.success == 'function' && params.success(res.data);
				},
				fail: (res) => {
					uni.hideLoading();
					if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
						uni.showToast({
							title: Config.baseUrl + '不在request 合法域名列表中',
							'icon': 'none',
							duration: 10000
						});
						return;
					}
					typeof params.fail == 'function' && params.fail(res);
				},
				complete: (res) => {
					if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(
							res.statusCode) == -1)) {
						uni.showToast({
							title: Config.baseUrl + '请求失败',
							'icon': 'none',
							duration: 10000
						});
						return;
					}
					typeof params.complete == 'function' && params.complete(res.data);
				}
			});
		}
	},
	upload(params) {
		uni.showLoading({
			title: '上传中...',
			mask: true
		});
		uni.uploadFile({
			url: Config.baseUrl + params.url + '?driver=' + siteConfig.upload.mode,
			filePath: params.filePath,
			name: params.name || 'file',
			fileType: params.fileType || 'image',
			formData: {
				app_type,
				app_type_name,
			},
			header: {
				'lr-dealer-token': uni.getStorageSync('token')
			},
			success: (res) => {
				uni.hideLoading();
				typeof params.success == 'function' && params.success(JSON.parse(res.data));
			},
			fail: (res) => {
				uni.hideLoading();
				typeof params.fail == 'function' && params.fail(res);
			}
		});
	}
}